package com.cuiweiyou.libraryrouter;

import android.app.Activity;
import android.content.Intent;

import java.util.HashMap;
import java.util.Map;

public class Router {

    private static Router instance;
    private Map<String, Class<? extends Activity>> activityMap = new HashMap<>();

    private Router() {
    }

    public static Router getInstance() {
        if (null == instance) {
            synchronized (Router.class) {
                if (null == instance) {
                    instance = new Router();
                }
            }
        }
        return instance;
    }

    /**
     * 注册aty
     * @param path 想要启动的aty的路径，形式：'/login/LoginActivity'
     * @param clazz aty对应的class
     */
    public void register(String path, Class clazz){
        activityMap.put(path, clazz);
    }

    /**
     * 启动aty
     * @param srcActivity
     * @param path
     */
    public void startActivity(Activity srcActivity, String path){
        Class<? extends Activity> clazz = activityMap.get(path);
        if (null != clazz){
            Intent i = new Intent(srcActivity, clazz);
            srcActivity.startActivity(i);
        }
    }
}
